package Action.user;

import Bean.BookCommentEntity;
import Bean.BookInfoEntity;
import Bean.Log.HistoricalData;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import tools.HibernateTools;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.spi.http.HttpContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.LinkedList;
import java.util.List;

public class ShowBookDetail extends ActionSupport {

    int modify_id;
    BookInfoEntity bookInfoEntity;
    List<BookCommentEntity> bookCommentEntities;
    List<BookCommentEntity> goodComments;
    List<BookInfoEntity> relateBooks;
    String commentsize;
    String comment;
    String err;
    @Override
    public String execute() throws Exception {
//        clearActionErrors();
        if (err!=null)
            addActionError(err);
        Session session= HibernateTools.getSession();
        bookInfoEntity=session.get(BookInfoEntity.class, modify_id);
        if (bookInfoEntity==null){
            return ERROR;
        }
        bookInfoEntity.setVister(bookInfoEntity.getVister()+1);
        session.update(bookInfoEntity);
        bookCommentEntities=session.createQuery("from BookCommentEntity where bookId=? order by id desc ").setParameter(0, bookInfoEntity.getId()).setMaxResults(1000).getResultList();
        commentsize=""+(bookCommentEntities.size()==1000?"999+":bookCommentEntities.size());
        goodComments=new LinkedList<>();
        for (BookCommentEntity bookCommentEntity : bookCommentEntities) {
            if (bookCommentEntity.getComment()!=null&&bookCommentEntity.getComment().length()>=7&&bookCommentEntity.getComment().length()<20){
                goodComments.add(bookCommentEntity);
            }
        }
        session.getTransaction().commit();

        try {
            bookInfoEntity.build_image(false);
        } catch (Exception e){
            System.err.println("图片生成失败");
        }

        //相关书籍算法:-----
        relateBooks=bookInfoEntity.related_books(5);
        for (BookInfoEntity relateBook : relateBooks) {
            try {
                relateBook.build_image(false);
            } catch (Exception e) {
                System.err.println("图片生成失败");
            }
        }
        return SUCCESS;
    }

    public String addComment () throws Exception {
        clearErrorsAndMessages();
        Session session=HibernateTools.getSession();
        bookInfoEntity=session.get(BookInfoEntity.class, modify_id);
        if (comment==null||comment.equals("")){
          err="评论不能为空";
          addActionMessage(err);
//          execute();
          return INPUT;
        }
        BookCommentEntity commentEntity=new BookCommentEntity();
        commentEntity.setBookId(bookInfoEntity.getId());
        commentEntity.setUserId((int) ActionContext.getContext().getSession().get("userid"));
        commentEntity.setComment(comment);
        commentEntity.setDate(new Timestamp(System.currentTimeMillis()));


        session.save(commentEntity);
        HistoricalData historicalData=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (historicalData==null){
            historicalData=new HistoricalData();
            historicalData.setTimestamp(tools.TimeTools.formatTime(0));
        }
        historicalData.setCommentNum(historicalData.getCommentNum()+1);
        session.saveOrUpdate(historicalData);
        session.getTransaction().commit();
        addActionMessage("评论成功");
        return SUCCESS;
    }


    public int getModify_id() {
        return modify_id;
    }

    public void setModify_id(int modify_id) {
        this.modify_id = modify_id;
    }

    public BookInfoEntity getBookInfoEntity() {
        return bookInfoEntity;
    }

    public void setBookInfoEntity(BookInfoEntity bookInfoEntity) {
        this.bookInfoEntity = bookInfoEntity;
    }

    public List<BookCommentEntity> getBookCommentEntities() {
        return bookCommentEntities;
    }

    public void setBookCommentEntities(List<BookCommentEntity> bookCommentEntities) {
        this.bookCommentEntities = bookCommentEntities;
    }

    public String getCommentsize() {
        return commentsize;
    }

    public void setCommentsize(String commentsize) {
        this.commentsize = commentsize;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getErr() {
        return err;
    }

    public void setErr(String err) {
        this.err = err;
    }

    public List<BookCommentEntity> getGoodComments() {
        return goodComments;
    }

    public void setGoodComments(List<BookCommentEntity> goodComments) {
        this.goodComments = goodComments;
    }

    public List<BookInfoEntity> getRelateBooks() {
        return relateBooks;
    }

    public void setRelateBooks(List<BookInfoEntity> relateBooks) {
        this.relateBooks = relateBooks;
    }
}
